package com.isotxt;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LuceneReadIndexExample {
    private static final String INDEX_DIR = "D:\\library\\tomcat\\webapps\\researchFile\\index";

    private static TopDocs searchByAll(IndexSearcher searcher) throws Exception {
        MatchAllDocsQuery matchAllDocsQuery = new MatchAllDocsQuery();
        TopDocs hits = searcher.search(matchAllDocsQuery, Integer.MAX_VALUE);
//        TopDocs hits = searcher.search(matchAllDocsQuery, 30);
        return hits;
    }

    private static IndexSearcher createSearcher() throws IOException {
        Directory dir = FSDirectory.open(new File(INDEX_DIR));
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher searcher = new IndexSearcher(reader);
        return searcher;
    }

    public static void main(String[] args) throws Exception {
        IndexSearcher searcher = createSearcher();

        TopDocs foundDocs = searchByAll(searcher);

        System.out.println("Total Results :: " + foundDocs.totalHits);

        for (ScoreDoc sd : foundDocs.scoreDocs) {
            log.info("sd.doc={}", sd.doc);
            Document d = searcher.doc(sd.doc);
            Set fNames = d.getFields().stream().map(f -> f.name()).collect(Collectors.toSet());
            ArrayList<String> props = new ArrayList<>();
            for (String p : Arrays.asList("title", "library", "sdate", "author", "year", "date", "刊登期号", "副题", "文章引题", "期次", "栏目")) {
                if (fNames.contains(p)) {
                    String v = d.get(p);
                    if (v != null) {
                        props.add(v.trim());
                    }
                }
            }
            Path txtsDir = Paths.get("D:\\dl\\isotxt");
            txtsDir.toFile().mkdirs();
            String fn = sd.doc + "_" + String.join("_", props)
                    .replaceAll("[\"“”:*?<>]","");
            Path contentFile = Paths.get(txtsDir.toString(), fn + ".txt");
            FileUtils.writeStringToFile(contentFile.toFile(), d.get("content"), StandardCharsets.UTF_8);
        }

    }


}